#include <cstdlib>
#include <functional>
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x)
    {
        struct ListNode* lesshead, * lesstail;
        struct ListNode* greaterhead, * greatertail;
        lesshead = lesstail = (struct ListNode*)malloc(sizeof(struct ListNode));
        greaterhead = greatertail = (struct ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode* cur = pHead;
        while (cur)
        {
            if (cur->val < x)
            {
                lesstail->next = cur;
                lesstail = cur;
            }
            else
            {
                greatertail->next = cur;
                greatertail = cur;
            }
            cur = cur->next;
        }
        greatertail->next = nullptr;
        lesstail->next = greaterhead->next;
        pHead = lesshead->next;
        return pHead;
    }
};